package com.sdy.configcenter.task;

import com.sdy.configcenter.biz.model.WxOaAccessToken;
import com.sdy.configcenter.biz.service.WxOaAccessTokenService;
import com.sdy.mvc.annotation.TaskSync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class WxOaRefreshTask {
    @Autowired
    private WxOaAccessTokenService wxOaAccessTokenService;
    
    @Scheduled(cron = "0/30 * * * * ? ")
    @TaskSync
    public void oaRefreshTask() {
        List<WxOaAccessToken> l1 = wxOaAccessTokenService.lambdaQuery()
                .lt(WxOaAccessToken::getAccessTokenExpire, new Date())
                .or().isNull(WxOaAccessToken::getAccessToken)
                .or().eq(WxOaAccessToken::getAccessToken, "")
                .list();
        l1.forEach(wxOaAccessToken -> wxOaAccessTokenService.refreshGetAccessToken(wxOaAccessToken));

        List<WxOaAccessToken> l2 = wxOaAccessTokenService.lambdaQuery()
                .lt(WxOaAccessToken::getJsApiTicketExpire, new Date())
                .or().isNull(WxOaAccessToken::getJsApiTicket)
                .or().eq(WxOaAccessToken::getJsApiTicket, "")
                .list();
        l2.forEach(wxOaAccessToken -> wxOaAccessTokenService.refreshGetJsApiTicket(wxOaAccessToken));
    }
}
